home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Libraries / objects in c ƒ / main.c < prev    next >
Encoding:
C/C++ Source or Header  |  1989-03-07  |  6.7 KB  |  254 lines  |  [TEXT/KAHL]

  1. /*
  2.  *        Objects-In-C test program
  3.  *
  4.  *            Copyright © John Wainwright 1988
  5.  *
  6.  */
  7.  
  8. #include "oic.h"
  9. #include "generics.h"
  10. #include "names.h"
  11.  
  12. enum { TITLE = 1, BOUNDS, KIND };        
  13. extern class         Window;                
  14.  
  15. main()
  16. {
  17.     object            p1, p2, n1, n2, n3, n4;
  18.     object            box1, box2;
  19.     object            list1, list2, s1;
  20.     object            seq, item;
  21.     register int    i;
  22.     register long    time;
  23.     extern  class      Coord, Box, Window;
  24.     object            d, w;
  25.     static Rect     bounds = {100, 100, 350, 350};
  26.     GenericTable    *gen;
  27.  
  28.     MaxApplZone();
  29.     
  30.     /*
  31.      *  initialize OIC.
  32.      */
  33.     InitOIC();
  34.     InitSysClasses();
  35.     InitTestClasses();
  36.     InitNameClasses();
  37.  
  38. /*
  39.     TraceClass(List);
  40.     TraceOn();
  41. */
  42.  
  43.     /*
  44.      *  play with strings & lists.  This code exercises the object 
  45.      *  making routines.  It makes a few strings & lists of strings
  46.      *  & uses the generic "print" to dump them out.
  47.      */
  48.      
  49.     print(New(String, "hello world"));
  50.     list1 = New(List,
  51.                 New(String, "one"),
  52.                 New(String, "two"),
  53.                 s1 = New(String, "three"), END);
  54.     print(list1);
  55.     print(head(list1));
  56.     print(tail(list1));
  57.     print(list1);
  58.     print(add(list1, New(String, "four"),
  59.             New(List,
  60.                New(String, "one"),
  61.                New(String, "two"),
  62.                New(String, "three"), END), END));
  63.                    
  64.     /* 
  65.      * ditto with Coords & Boxes - these are just some silly little
  66.      * classes for the test program to play with.  I wouldn't make
  67.      * real Point & Rect classes using OIC - you need extremely high
  68.      * bandwidth in manipulating them they're best left as C types
  69.      */
  70.      
  71.     print((p1 = New(Coord, 100.0, 100.0)));
  72.     offset(p1, 2.0, 4.0);
  73.     print(p1);
  74.     p2 = New(Coord, 200.0, 200.0);
  75.     print((box1 = New(Box, 100.0, 100.0, 200.0, 200.0)));
  76.     
  77.     /*
  78.      *  add different things to the original list - here we are
  79.      *  showing the heterogeneous nature of List - it can contain any
  80.      *  class of object as an element.  
  81.      */
  82.     add(list1, p1, box1, New(List, box1, p1, END), END);
  83.     print(list1);
  84.     print(push(list1, p1));
  85.     print(list1 = delete(list1, p1));
  86.     print(list1 = delete(list1, s1));
  87.     gprintf(screen, "%s %s\n", ClassNameOf(list1), ClassNameOf(p1));
  88.  
  89.     /*
  90.      * experiment with the sequencing class.  This is an
  91.      * EXTREMELY powerful generic sequencing mechanism 
  92.      * that fits cleanly with the C for statement. Any class
  93.      * that responds to the "sequence" generic can be thusly
  94.      * sequenced over.
  95.      */
  96.      
  97.     for (seq = sequence(list1); item = next(seq);)
  98.         print(item);
  99.     
  100.     /*
  101.      * the above can be done more simply using the "map" generic,
  102.      * inherited from "Object". It maps a function over each element
  103.      * (using the "sequence" generic) of the given object.
  104.      */
  105.      
  106.     map(list1, print);
  107.     
  108.     /*
  109.      *  "Coords" inherit from indexMixin which keeps track of
  110.      *  all instances - it provides a class method for "sequence"
  111.      *    to allow us to simply sequence over all instances.
  112.      *  see how neat it all is ?
  113.      */
  114.     gprintf(screen, "\nall Coord instances...\n");
  115.     for (seq = sequence(Coord); item = next(seq);)
  116.     {
  117.         gprintf(screen, "-> "); print(item);
  118.     }
  119.  
  120.     /*
  121.      * indexMixin can find ALL its kept instances ...
  122.      */
  123.     gprintf(screen, "\nall indexMixin deepinstances...\n");
  124.     for (seq = sequence(deepInstances(IndexMixin)); item = next(seq);)
  125.     {
  126.         gprintf(screen, "-> "); print(New(List, item, className(item), END));
  127.     }
  128.  
  129.     /*
  130.      * playing with other kind of list
  131.      */
  132.     gprintf(screen, "\ntrying alternate list classes...\n");
  133.     list2 = New(List2);
  134.     add(list2, New(String, "one"),
  135.                New(String, "two"),
  136.                New(String, "three"), END);
  137.     print(list2);
  138.     
  139.     /*
  140.      * timing differences in making the 2 different list kinds.  
  141.      * OIC is reasonably quick - here we have a 1000 element list being
  142.      * created & then sequenced through in about a second on a Mac II.
  143.      * Profiling has shown that about 45% of the time is spent (not
  144.      * suprisingly) in the mehtod dispatch function "Method".  Recoding
  145.      * it in assembler could bring substantial improvements.
  146.      */
  147.     gprintf(screen, "\nmaking a 1000 element list\n");
  148.     time= TickCount();
  149.     list1 = New(List, END);
  150.     for (i = 0; i < 1000; i++)
  151.         push(list1, p1);
  152.     gprintf(screen, "done... sequencing it...\n");
  153.     for (i = 0, seq = sequence(list1); item = next(seq); )
  154.         i++;
  155.     gprintf(screen, "done, found %d items, %ld ticks\n", i, TickCount() - time);
  156.     
  157.     gprintf(screen, "making a 1000 element list2\n");
  158.     time= TickCount();
  159.     list2 = New(List2);
  160.     for (i = 0; i < 1000; i++)
  161.         push(list2, p1);
  162.     gprintf(screen, "done... sequencing it...\n");
  163.     for (i = 0, seq = sequence(list2); item = next(seq); )
  164.         i++;
  165.     gprintf(screen, "done, found %d items, %ld ticks\n", i, TickCount() - time);
  166.     
  167.     /*
  168.      * the class Class has a number of useful methods ...
  169.      */
  170.     gprintf(screen, "\nall Object subclasses...\n");
  171.     print(subs(Object));
  172.     
  173.     gprintf(screen, "all Collect subclasses...\n");
  174.     print(subs(Collect));
  175.     
  176.     gprintf(screen, "all List2 supers...\n");
  177.     print(supers(List2));
  178.     
  179.     /*
  180.      * check out the fledgling imbedded Lisp classes ....
  181.      * at the moment only the early name space classes are present
  182.      * in rough form.   The HashTable, NameSpace, & Name classes might be
  183.      * useful on their own.
  184.      */
  185.      
  186.     p1 = New(NameSpace, 127, 0);
  187.     print(p1);
  188.     n1 = declare(Name, "fred");
  189.     print(n1);
  190.     bind(p1, n1, New(List, New(String, "123"), END));
  191.     print(p1);
  192.     print(get(p1, n1));
  193.     gprintf(screen, "%lx, %lx, %lx\n", n1, declare(Name, "fred"), declare(Name, "Joe"));
  194.     bind(p1, declare(Name, "Bill"), box1);
  195.     
  196.     for (seq = sequence(p1); item = next(seq); )
  197.     {
  198.         gprintf(screen, "%s: ", stringOf(item));
  199.         print(get(p1, item));
  200.     }
  201.     
  202.     /*
  203.      * play around with the very simple window class. Note that
  204.      * it uses List as a mixin to provide a simple contents list.
  205.      * I am also experimenting with keyword parameters, a la Lisp -
  206.      * see window.c see & key_arg() in oic.h for details
  207.      */
  208.     w = New(Window, BOUNDS, &bounds,
  209.                     TITLE, "\pJohn's",
  210.                     KIND, (long)rDocProc,
  211.                     END);
  212.     
  213.     add(w, box1, New(Box, 150.0, 150.0, 190.0, 190.0), END);
  214.     draw(w);
  215.     print(w);
  216.     
  217.     /*
  218.      * check the DependentsMixin class.  It is intended as a
  219.      * change propgation system.  Class Box inherits from 
  220.      * DependentsMixin: any instance can have dependents declared.
  221.      */
  222.     gprintf(screen, "check change propogation\n");
  223.     box2 = New(Box, 10.0, 10.0, 20.0, 20.0);
  224.     addDependent(box2, box1);
  225.     offset(box2, 1.0, 4.0);
  226.     
  227.     /*
  228.      * check the "cantDo", "CanYouDo" mechanism
  229.      */
  230.     gprintf(screen, "check the cantDo mehcanism\n");
  231.     append(s1);
  232.     next(list1);
  233.     
  234.     gprintf(screen, "%s append\n", CanYouDo(s1, appendGeneric) ? "I can do " : "I can't do ");
  235.     gprintf(screen, "%s equal\n", CanYouDo(s1, equalGeneric) ? "I can do " : "I can't do ");
  236.     
  237.     /*
  238.      * print the list of generics
  239.      */
  240.      
  241.     gprintf(screen, "the generics ...\n\n");
  242.     for (i = 0, gen = generics; gen != NULL; i++, gen = gen->gen_next)
  243.         gprintf(screen, "%d: %s()\n", i, GenericName(gen));
  244.         
  245.     gprintf(screen, "all done\n");
  246. }
  247.  
  248. InitTestClasses()
  249. {
  250.     InitCoordClass();
  251.     InitBoxClass();
  252.     InitWindowClass();
  253. }    
  254.